home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 March / macformat-022.iso / Shareware City / Graphics / SPD / Sources / readdxf.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-05-10  |  6.6 KB  |  275 lines  |  [TEXT/R*ch]

  1. /*
  2.  * ReadDXF.c - Read polygons from a DXF file and display them.
  3.  * The file "view.dat" is processed for the camera view that will be used.
  4.  *
  5.  * Author:  Alexander Enzmann
  6.  *
  7.  * size_factor is ignored.
  8.  *
  9.  *    size_factor       # spheres        # squares
  10.  *         x               xx                 x
  11.  */
  12.  
  13. #include <stdio.h>
  14. #include <math.h>
  15. #include <stdlib.h>     /* atoi */
  16. #include <string.h>     /* strcmp() */
  17. #include "def.h"
  18. #include "drv.h"       /* display_close() */
  19. #include "lib.h"
  20.  
  21.  
  22. /* These may be read from the command line */
  23. static int raytracer_format = OUTPUT_VIDEO;
  24. static int output_format    = OUTPUT_CURVES;
  25.  
  26.  
  27. #ifdef OUTPUT_TO_FILE
  28. static FILE * stdout_file = NULL;
  29. #else
  30. #define stdout_file stdout
  31. #endif /* OUTPUT_TO_FILE */
  32.  
  33.  
  34. /* This is an outrageous hack to read the polygons from a DXF file.
  35.    No attempt is made to be smart, just to graph 3DFACEs.  If you have
  36.    something better, go for it. There are plenty of DXF files that this
  37.    routine won't work for... */
  38. static void
  39. read_dxf_faces( file )
  40. FILE *file;
  41. {
  42.     char buffer[128];
  43.     int i, j, ind, vcnt, fcnt, lineno, maxvert;
  44.     float x;
  45.     COORD3 face[16];
  46.  
  47.     fcnt = lineno = 0;
  48.     while (!feof(file)) {
  49.  
  50.     /* Skip over uninteresting stuff */
  51.     while (!feof(file) &&
  52.          fgets(buffer, 127, file) != NULL &&
  53.          strcmp(buffer, "3DFACE\n"))
  54.         /* Void - we are reading lines we can't deal with */
  55.         lineno++;
  56.  
  57.     if (!feof(file)) {
  58.  
  59. #if defined(applec) || defined(THINK_C)
  60. #else
  61.         /* Test to see if we should stop */
  62.         if (kbhit()) {
  63.         display_close(0);
  64.         fprintf(stderr, "Draw aborted\n");
  65.         exit(EXIT_FAIL);
  66.         }
  67. #endif
  68.  
  69.         /* As long as there are more faces, read them in */
  70.         fgets(buffer, 127, file); /* Skip the "8" */
  71.         lineno++;
  72.  
  73.         fgets(buffer, 127, file); /* Skip the "0main" */
  74.         lineno++;
  75.  
  76.         if (buffer[0] == '0')
  77.         ;
  78.         else if (!strcmp(buffer, "3DFURN\n")) {
  79.         fgets(buffer, 127, file); /* Skip the thing after the 3DFURN */
  80.         fgets(buffer, 127, file); /* Skip over the CONTINUOUS */
  81.         lineno += 2;
  82.         if (!strcmp(buffer, "CONTINUOUS\n") &&
  83.              fgets(buffer, 127, file) != NULL &&
  84.              sscanf(buffer, "%d", &ind) != 0) {
  85.             lineno++;
  86.             if (ind == 62) {
  87.             fgets(buffer, 127, file); /* Max # of vertices? */
  88.             sscanf(buffer, "%d", &maxvert);
  89.             fgets(buffer, 127, file);
  90.             sscanf(buffer, "%d", &ind);
  91.             lineno++;
  92.             } else {
  93.             maxvert = -1;
  94.             }
  95.             fgets(buffer, 127, file);
  96.             sscanf(buffer, "%f", &x);
  97.             lineno++;
  98.             goto inside_vertex_loop;
  99.         }
  100.         else
  101.             break;
  102.         } else if (!strcmp(buffer, "2\n")) {
  103.         fgets(buffer, 127, file);
  104.         sscanf(buffer, "%d", &ind);
  105.         lineno++;
  106.         if (ind == 62) {
  107.             fgets(buffer, 127, file); /* Max # of vertices */
  108.             sscanf(buffer, "%d", &maxvert);
  109.             fgets(buffer, 127, file);
  110.             sscanf(buffer, "%d", &ind);
  111.             lineno++;
  112.         } else {
  113.             maxvert = -1;
  114.         }
  115.         fgets(buffer, 127, file);
  116.         sscanf(buffer, "%f", &x);
  117.         lineno++;
  118.         goto inside_vertex_loop;
  119.         } else
  120.         break;
  121.  
  122.         lineno += 3;
  123.         vcnt = 0;
  124.         maxvert = -1;
  125.  
  126.         /* This is a face, read the vertices */
  127.         while (fgets(buffer, 127, file) != NULL &&
  128.          (sscanf(buffer, "%d", &ind) != 0) &&
  129.          ind != 0 &&
  130.          fgets(buffer, 127, file) != NULL &&
  131.          sscanf(buffer, "%f", &x)) {
  132.  
  133.         PLATFORM_MULTITASK();
  134.  
  135.         lineno += 2;
  136. inside_vertex_loop:
  137.         /* Got a vertex value */
  138.         j = ind / 10;
  139.         i = ind % 10;
  140.  
  141.         if (maxvert > 0 && i > maxvert)
  142.             break;
  143.         else if (i > vcnt)
  144.             vcnt = i;
  145.  
  146.         /* Place the value into the appropriate face */
  147.         if (j == 1)
  148.             face[i][X] = x;
  149.         else if (j == 2)
  150.             face[i][Y] = x;
  151.         else if (j == 3)
  152.             face[i][Z] = x;
  153.         else if (maxvert > 0)
  154.             break;
  155.         else {
  156.             display_close(1);
  157.             fprintf(stderr, "Bad vertex component: %d/%d at line: %d\n",
  158.                   i, j, lineno);
  159.             exit(EXIT_FAIL);
  160.         }
  161.         }
  162.         /* Display the polygon */
  163.         if (vcnt > 0)
  164.         lib_output_polygon(vcnt+1, face);
  165.         /*
  166.         printf("Vert[%d]: ", fcnt);
  167.         for (i=0;i<vcnt;i++)
  168.         printf("<%g, %g, %g> ", face[i][X], face[i][Y], face[i][Z]);
  169.         printf("\n");
  170.         */
  171.         fcnt++;
  172.     }
  173.     }
  174. }
  175.  
  176. /* Read in the camera specifics: from, at, up, fov.  Aspect is hard coded
  177.     to 1.  */
  178. static void
  179. setup_view()
  180. {
  181.     char buffer[128];
  182.     COORD3 from, at, up;
  183.     double angle;
  184.     FILE *setup;
  185.  
  186.     /* output viewpoint */
  187.     if ((setup = fopen("view.dat", "r")) != NULL) {
  188.     if (fgets(buffer, 127, setup) &&
  189.          sscanf(buffer, "%lf %lf %lf",
  190.               &from[X], &from[Y], &from[Z]) != 0 &&
  191.          fgets(buffer, 127, setup) &&
  192.          sscanf(buffer, "%lf %lf %lf",
  193.               &at[X], &at[Y], &at[Z]) != 0 &&
  194.          fgets(buffer, 127, setup) &&
  195.          sscanf(buffer, "%lf %lf %lf",
  196.               &up[X], &up[Y], &up[Z]) != 0 &&
  197.          fgets(buffer, 127, setup) &&
  198.          sscanf(buffer, "%lf", &angle)) {
  199.         lib_output_viewpoint(from, at, up, 45.0, 1.0, 1.0, 512, 512);
  200.     } else {
  201. #if defined(applec) || defined(THINK_C)
  202. #else
  203.         fprintf(stderr, "Invalid 'view.dat' file\n");
  204. #endif
  205.         exit(EXIT_FAIL);
  206.     }
  207.     fclose( setup );
  208.     } else {
  209.     SET_COORD3(from, 0, 10, -10);
  210.     SET_COORD3(at, 0, 0, 0);
  211.     SET_COORD3(up, 0, 0, 1);
  212.     lib_output_viewpoint(from, at, up, 45.0, 1.0, 1.0, 512, 512);
  213.     }
  214. }
  215.  
  216. /* Read in the camera view, then read in DXF polygons, then display them. */
  217. int
  218. main(argc, argv)
  219.     int argc;
  220.     char *argv[];
  221. {
  222.     COORD3 back_color, dxf_color;
  223.     COORD4 light;
  224.     double lscale;
  225.     char file_name[64] ;
  226.     FILE *file;
  227.  
  228.     PLATFORM_INIT(SPD_READDXF);
  229.  
  230.     /* Start by defining which raytracer we will be using */
  231.     if ( lib_read_get_opts( argc, argv,
  232.             &raytracer_format, &output_format, file_name ) ) {
  233.     return EXIT_FAIL;
  234.     }
  235.     if ( lib_open( raytracer_format, "ReadDXF.out" ) ) {
  236.     return EXIT_FAIL;
  237.     }
  238.  
  239.     file = fopen(file_name, "r");
  240.     if (file == NULL) {
  241.     fprintf(stderr, "Cannot open dxf file: '%s'\n", file_name);
  242.     return EXIT_FAIL;
  243.     }
  244.  
  245.     lib_set_polygonalization(3, 3);
  246.  
  247.     /* output background color - dark blue */
  248.     /* NOTE: Do this BEFORE lib_output_viewpoint(), for display_init() */
  249.     SET_COORD3(back_color, 0.1, 0.0, 0.5);
  250.     lib_output_background_color(back_color);
  251.  
  252.     setup_view();
  253.  
  254.     /* output object color - light gray */
  255.     SET_COORD3(dxf_color, 0.8, 0.8, 0.8);
  256.     lib_output_color(NULL, dxf_color, 0.1, 0.8, 0.0, 0.2, 5.0, 0.0, 1.0);
  257.  
  258.     /* output light sources */
  259.     lscale = (raytracer_format == OUTPUT_NFF ||
  260.           raytracer_format == OUTPUT_RTRACE ? 1.0 : 1.0 / sqrt(2.0));
  261.     SET_COORD4(light, 40.0, 30.0, 20.0, lscale);
  262.     lib_output_light(light);
  263.     SET_COORD4(light, -40, -20, 10, lscale);
  264.     lib_output_light(light);
  265.  
  266.     read_dxf_faces(file);
  267.  
  268.     fclose(file);
  269.  
  270.     lib_close();
  271.  
  272.     PLATFORM_SHUTDOWN();
  273.     return EXIT_SUCCESS;
  274. }
  275.